<%@ page import="sun.io.Converters" %>
<%@ page import="servlets.*" %>
<%@ page import="java.sql.SQLException" %>
<%--
  Created by IntelliJ IDEA.
  User: Raz0r
  Date: 30.11.12
  Time: 0:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    Integer status = Integer.parseInt(request.getParameter("status"));
    long insertedOrderId = -1;
    try {
               insertedOrderId = DBProvider.insertRecordIntoTable("INSERT INTO Order_"
                        + "(OrderStateId, BeginDate, Priority, TotalSum) VALUES(" +
                        0 + ", " +
                        0 + ", " +
                        status + ", " +
                        0 + ")");
            }catch (SQLException e) {

                System.out.println(e.getMessage());

            }
                           /*
    Order newOrder = new Order();
    newOrder.setOrderStates(0);
    newOrder.setPriority(status);
    newOrder.setBeginDate(22);
    */
    //Integer insertedOrderId = OrderList.addOrder(newOrder);

    Integer itemNumber = 0;
    String productId = request.getParameter("items[" + itemNumber + "][id]");
    while(productId != null)
    {
        Integer productCount = Integer.parseInt(request.getParameter("items[" + itemNumber + "][qty]"));

        for(int i = 0; i < productCount; i++)
        {
            try {
               long insertedOrderProductTableId = DBProvider.insertRecordIntoTable("INSERT INTO OrderProductTable"
                        + "(OrderId, ProductId, StateOrderProductId, OrderHistoryId) VALUES(" +
                        insertedOrderId + ", " +
                        Integer.parseInt(productId) + ", " +
                        0 + ", " +
                        0 + ")");
            }catch (SQLException e) {

                System.out.println(e.getMessage());

            }

                          /*
            OrderProductTable newOrderProductTable = new OrderProductTable();

            newOrderProductTable.setOrders(insertedOrderId);
            newOrderProductTable.setOrdersHistory(0);
            newOrderProductTable.setStatesOrderProducts(0);
            newOrderProductTable.setProducts();

            OrderProductTableList.addOrderProductTable(newOrderProductTable);   */
        }

        itemNumber++;
        productId = request.getParameter("items[" + itemNumber + "][id]");
    }

    out.print("{\"code\":\"0\"}");
%>